package com.exchange.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    public static final String QUEUE_INFORM_JOB="queue_inform_job";
    public static final String QUEUE_INFORM_TRANS="queue_inform_trans";
    public static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";
    public static final String ROUTINGKEY_JOB="inform.#.job.#";
    public static final String ROUTINGKEY_TRANS="inform.#.trans.#";

    //声明交换机
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange EXCHANGE_TOPICS_INFORM(){
        //.durable(true)   持久化, mq重启后交换机还在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }


    //声明队列
    //声明 作业 的队列
    @Bean(QUEUE_INFORM_JOB)
    public Queue QUEUE_INFORM_JOB(){
        return new Queue(QUEUE_INFORM_JOB);
    }
    //声明 转换 的队列
    @Bean(QUEUE_INFORM_TRANS)
    public Queue QUEUE_INFORM_TRANS(){
        return new Queue(QUEUE_INFORM_TRANS);
    }

    //绑定交换机与队列
    // 绑定 作业的队列与交换机
    @Bean
    public Binding BINDING_QUEUE_INFORM_JOB(@Qualifier(QUEUE_INFORM_JOB)Queue queue, @Qualifier(EXCHANGE_TOPICS_INFORM)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_JOB).noargs();
    }

    // 绑定 转换的队列与交换机
    @Bean
    public Binding BINDING_QUEUE_INFORM_TRANS(@Qualifier(QUEUE_INFORM_TRANS)Queue queue, @Qualifier(EXCHANGE_TOPICS_INFORM)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_TRANS).noargs();
    }
}
